<template>
  <div class="m-content">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="5%" class="demo-ruleForm">
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title" placeholder="请输入标题..." ref="inputs"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <mavon-editor v-model="ruleForm.content" placeholder="请输入内容..." style="z-index:1;border: 1px solid #d9d9d9;height:74vh"></mavon-editor>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">分享</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import "github-markdown-css"
import {addShare} from "../../network/Details/share";
export default {
  name: "PublishShare",
  data(){
    return {
      ruleForm: {
        title: '',
        content: '',
        username: window.sessionStorage.getItem("username")
      },
      rules: {
        title: [
          {required: true, message: '请输入标题', trigger: 'blur'},
          {min: 3, max: 100, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        content: [
          {required: true, message: '请输入内容', trigger: 'blur'}
        ],

      },

    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addShare(this.ruleForm,'post').then(res => {
            this.$alert('保存成功', '提示', {
              confirmButtonText: '确定',
              callback: action => {
                this.$router.push({
                  name:"ShareArea",
                  query:{
                    index: '/shareArea'
                  }
                })
              }
            })
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

  }
}
</script>

<style scoped>
.m-content {
  width: 98%;
  margin-top: 2%;
}
</style>
